package ui;

import java.awt.*;

public class ScreenChar {

  private int [] color;
  private int [] animation;
  private int [] background;
  private int [] overlay;
  private char character;
  private int charWidth, charHeight;
  private boolean center, useGlyph;
  private Font font, glyph;

  public ScreenChar(int w, int h, Font f, Font g) {
    character = ' ';
    color = null;
    animation = null;
    background = null;
    overlay = null;
    center = false;
    useGlyph = false;
    charWidth = w;
    charHeight = h;
    font = f;
    glyph = g;
  }

  public char getChar() {
    return(character);
  }

  public int [] getColor() {
    return(color);
  }

  public void setChar(char c, int [] col) {
    character = c;
    color = col;
    animation = null;
    background = null;
    overlay = null;
    //center = false;
    useGlyph = false;
  }

  public void setCenter() {
    center = true;
  }

  public void setGlyph() {
    useGlyph = true;
  }

  public void setBackground(int [] col) {
    background = col;
  }

  public void setAnimation(int [] col) {
    animation = col;
  }

  public void display(Graphics g, int x, int y) {

    int cx, cy;
    double dh, dw;
    char [] cs = { character };
    String s = new String(cs);

    cx = cy = 0;
    g.setFont(font);
    /* character specifc adjustments */
    if(character == '(' || character == ')') {
//      cx = cx - 1;
//      cy = cy - 1;
    }
//    else if(character == '<' || character == '>') cx = cx + 1;
    else if(character == (char) 65312) { /* big @ */
      //g.setFont(glyph);
      g.setFont(font.deriveFont(18f));
      //cx = cx + 1;
      cy = cy + 2;
    } else if(character == (char) 9827) { /* club suite (tree) */
      //g.setFont(glyph);
    } else if (character == (char) 8776) { /* double tilde (water) */
      g.setFont(font.deriveFont(24f));
    }

    FontMetrics fm = g.getFontMetrics();
    dw = fm.stringWidth(s);
    dh = fm.getAscent() - 4;

    if(center) {
      cx = cx + x + (int) Math.floor(((double) charWidth - dw) / 2.0);
      cy = cy + y + charHeight - (int) Math.ceil(((double) charHeight - dh) / 2.0);
    } else {
      /*
      cx = x - 1;
      cy = y + charHeight - 4;
      */
      cx = cx + x;
      cy = cy + y + charHeight;
    }

    if(background != null) {
      g.setColor(toColor(background));
      g.fillRect(x, y, charWidth, charHeight);
    }

    if(color != null) g.setColor(toColor(color));
    else g.setColor(Color.black);
    g.drawString(s, cx, cy);

  }

  private Color toColor(int [] i) {
    return(new Color(i[0], i[1], i[2]));
  }

}
